package com.ajr.core;

import java.util.HashSet;
import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * @author Deepak Kumar
 * 
 *         http://www.roseindia.net HQL Where Clause Example Where Clause With
 *         Select Clause Example
 */
public class OrderClauseExample {
	private static int ROW_COUNT = 50;

	public static String readOsCommerceOrders(HashSet vismaOrders) {
		Session session = null;

		try {
			// This step will read hibernate.cfg.xml and prepare hibernate for
			// use
			SessionFactory sessionFactory = new Configuration().configure()
					.buildSessionFactory();
			session = sessionFactory.openSession();

			// System.out.println("*******************************");
			// System.out.println("Query using Hibernate Query Language");
			// Query using Hibernate Query Language
			String SQL_QUERY = " from Orders where orders_id = 7139"; // as insurance where
			// insurance.lngInsuranceId='1'
			Query query = session.createQuery(SQL_QUERY);
			int c = 0;
			StringBuffer sbf = new StringBuffer("<table>");
			for (Iterator it = query.iterate(); it.hasNext();) {
				Orders insurance = (Orders) it.next();
				String ordernumber = String.valueOf(insurance.getOrders_id());
				if (!vismaOrders.contains(ordernumber)) {
					sbf.append("<tr>");
					sbf
							.append("<td><input type=checkbox name=orderselected checked></td>");
					sbf.append("<td>" + ordernumber + "</td>");
					sbf
							.append("<td>" + insurance.getCustomers_name()
									+ "</td>");
					sbf
							.append("<td>" + insurance.getDate_purchased()
									+ "</td>");
					sbf.append("<td>" + insurance.getOrders_status() + "</td>");
					// sbf.append(insurance.getOrders_id() + "-" +
					// insurance.getCustomers_id() +"-" +
					// insurance.getCustomers_name()+"-"+insurance.getDate_purchased()+"-"+insurance.getOrders_status());
					// System.out.println("Name: " +
					// insurance.getCustomers_firstname());
					sbf.append("</tr>");
					
					c++;
					if (c >= ROW_COUNT)
						break;
					
				}
			}
			sbf.append("</table>");
			// System.out.println("*******************************");
			/*
			 * System.out.println("Where Clause With Select Clause"); //Where
			 * Clause With Select Clause SQL_QUERY
			 * ="Select insurance.lngInsuranceId,insurance.insuranceName," +
			 * "insurance.investementAmount,insurance.investementDate from Insurance insurance "
			 * + " where insurance.lngInsuranceId='1'"; query =
			 * session.createQuery(SQL_QUERY); for(Iterator
			 * it=query.iterate();it.hasNext();){ Object[] row = (Object[])
			 * it.next(); System.out.println("ID: " + row[0]);
			 * System.out.println("Name: " + row[1]);
			 * 
			 * }
			 */
			// System.out.println("*******************************");

			session.close();
			return sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		} finally {
		}
		return null;
	}
	
	public static void main(String[] args) {
		HashSet hashMap = new HashSet();
		hashMap.add("7139");
		String s = readOsCommerceOrders(hashMap);
		System.out.println(s);
	}
	
}
